package com.haochang.chunkvod.controller.adapter.cash;

import java.util.ArrayList;
import java.util.List;

import com.haochang.base.BaseActivity;
import com.haochang.base.BaseListView;
import com.haochang.base.BaseTextView;
import com.haochang.base.OnBaseClickListener;
import com.haochang.chunkvod.R;
import com.haochang.chunkvod.app.common.intent.IntentKey;
import com.haochang.chunkvod.app.utils.SaleUtils;
import com.haochang.chunkvod.app.utils.ToastUtils;
import com.haochang.chunkvod.controller.activity.cash.SettlementActivity;
import com.haochang.chunkvod.controller.adapter.base.BaseAdapterHelper;
import com.haochang.chunkvod.controller.adapter.base.QuickAdapter;
import com.haochang.chunkvod.model.cash.GoodInfo;
import com.haochang.chunkvod.model.cash.NonOperateEnum;
import com.haochang.chunkvod.model.cash.TakeOutInfo;
import com.haochang.chunkvod.model.hostess.HostessData;
import com.haochang.chunkvod.model.hostess.HostessInfo;
import com.haochang.chunkvod.tools.vod.other.DipPxConversion;
import com.haochang.chunkvod.view.vod.dialog.ChooseRoomDialog;
import com.haochang.chunkvod.view.vod.dialog.VodCustomDialog;

import android.content.Context;
import android.content.Intent;
import android.view.View;

/**
 * 挂单详情适配器
 */
public class PendOperateDetailsAdapter extends QuickAdapter<TakeOutInfo>
{
	private Context mContext;
	private List<HostessInfo> hostessInfoList;
	private HostessData hostessData;

	public PendOperateDetailsAdapter(Context context, int layoutResId, List data)
	{
		super(context, layoutResId, data);
		mContext = context;
		hostessData = new HostessData((BaseActivity) mContext);
	}

	@Override
	protected void convert(BaseAdapterHelper helper, final TakeOutInfo item)
	{
		helper.setText(R.id.tv_serialNumber, item.getSerialNumber());
		helper.setText(R.id.tv_roomNum, item.getRoomNum());
		helper.setText(R.id.tv_source, item.getSource());
		helper.setText(R.id.tv_price, SaleUtils.getSaleString(item.getTotalPrice()));
		helper.getView(R.id.tv_edit).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.tv_edit).setTag(helper);
		helper.getView(R.id.tv_pend_room).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.tv_checkOut).setOnClickListener(onBaseClickListener);
		helper.getView(R.id.tv_goodsDetail).setOnClickListener(onBaseClickListener);
	}

	/**
	 * 请求空房list
	 */
	private void requestEmptyRoomList()
	{
		hostessData.requestEmptyRoomList(new HostessData.ResultResponseList<HostessInfo>()
		{
			@Override
			public void onRequestSuccess(List<HostessInfo> list)
			{
				hostessInfoList = list;
			}

			@Override
			public void onFail(int errno, String errMsg)
			{
			}
		});
	}

	private OnBaseClickListener onBaseClickListener = new OnBaseClickListener()
	{
		@Override
		public void onBaseClick(View v)
		{
			switch (v.getId())
			{
			case R.id.tv_edit:
				BaseAdapterHelper helper = (BaseAdapterHelper) v.getTag();
				if (helper.getView(R.id.bottom_line).getVisibility() == View.GONE)
				{
					helper.getView(R.id.bottom_line).setVisibility(View.VISIBLE);
					helper.getView(R.id.bottom_div).setVisibility(View.VISIBLE);
					((BaseTextView) v).setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.up_arrows), null);
				} else
				{
					helper.getView(R.id.bottom_line).setVisibility(View.GONE);
					helper.getView(R.id.bottom_div).setVisibility(View.GONE);
					((BaseTextView) v).setCompoundDrawablesWithIntrinsicBounds(null, null, context.getResources().getDrawable(R.drawable.down_arrows), null);
				}
				break;
			case R.id.tv_pend_room:
				//选择房型
				if (null == hostessInfoList)
				{
					ToastUtils.showText(mContext, "正在获取房间列表...");
					requestEmptyRoomList();
					return;
				}
				new ChooseRoomDialog.Builder(mContext).setTitle("选择房间").setSingleChooseItems(hostessInfoList, new ChooseRoomDialog.OnSingleChoosedListener()
				{
					@Override
					public void onSingleChoosed(HostessInfo info)
					{
					}
				}).showDialog(8, 20);
				break;
			case R.id.tv_checkOut:
				Intent intent = new Intent(context, SettlementActivity.class);
				intent.putExtra(IntentKey.PRICEVALUE, 500);
				intent.putParcelableArrayListExtra(IntentKey.GOODS_LIST, new ArrayList<GoodInfo>());
				intent.putExtra(IntentKey.ACTIVITY_ENUM, NonOperateEnum.NON_OPERATE);
				context.startActivity(intent);
				break;
			case R.id.tv_goodsDetail:
				new VodCustomDialog.Builder(context).setTitle("商品详情").showDialog(R.layout.inflate_vod_custom_dialog_listview,
						DipPxConversion.dip2px(context, 40 * 8), 20, new VodCustomDialog.OnViewAddedListener()
						{
							@Override
							public void getView(View view)
							{
								List<GoodInfo> list = new ArrayList<GoodInfo>();
								list.add(new GoodInfo());
								list.add(new GoodInfo());
								list.add(new GoodInfo());
								BaseListView listView = (BaseListView) view.findViewById(R.id.listView);
								listView.setDivider(null);
								listView.setAdapter(new QuickAdapter<GoodInfo>(context, R.layout.item_cash_goods_detail, list)
								{
									@Override
									protected void convert(BaseAdapterHelper helper, GoodInfo object)
									{
										helper.setText(R.id.tv_title, "牛肉面");
										helper.setText(R.id.tv_count, "1份");
										helper.setText(R.id.tv_price, "¥200");
									}
								});
							}
						});
				break;
			default:
				break;
			}
		}
	};

}
